import React, { Component } from 'react'
import {Switch,Redirect,Route} from 'react-router-dom'

export class RouterViews extends Component {
  render() {
    let {routerConfig} = this.props
    let routerRoutes = routerConfig.filter(item => !item.redirect)
    let routerRedirect = routerConfig.filter(item => item.redirect)
    return (
      <Switch>
        {
          routerRoutes.map((item,index)=>{
            return <Route 
              key={index} 
              path={item.path}
              render={(props)=>{
                document.title=item.title
                if(item.children){
                  return <item.component {...props} routerConfig={item.children}/>
                }
                return <item.component {...props}/>
              }}
            />
          })
        }
        {
          routerRedirect.map((item,index)=>{
            return <Redirect 
              key={index}
              from={item.path}
              to={item.redirect}
            />
          })
        }
      </Switch>
    )
  }
}

export default RouterViews


// import React, { Component } from 'react'
// import {Switch,Redirect,Route} from 'react-router-dom'

// export class routerView extends Component {
  
//   render() {
//     let {routerConfig} = this.props
//     let routerRoutes = routerConfig.filter(item=>!item.redirect)
//     let routerRedirect = routerConfig.filter(item=>item.redirect)
//     return (
//       <Switch>
//         {
//           routerRoutes.map((item,index)=>{
//             return <Route 
//                     key={index}
//                     path={this.path}
//                     render={(props)=>{
//                       if(item.children){
//                         return <item.component {...props} routerConfig={item.children} />
//                       }
//                       return <item.component {...props} {...item} />
//                     }}
            
//             />
//           })
//         }
//         {
//           routerRedirect.map((item,index)=>{
//             return <Redirect 
//                     key={index}
//                     from={item.path}
//                     to={item.redirect}
//             />
//           })
//         }
//       </Switch>
//     )
//   }
// }

// export default routerView
